<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity4">
<head>
    <title>登录日志记录</title>
    <div th:replace="common/link::header"></div>
</head>
<body>
<div class="layui-fluid">
    <div class="layui-card">
        <form class="layui-form layui-card-header layuiadmin-card-header-auto">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">账号</label>
                    <div class="layui-input-block">
                        <input type="text" name="account" placeholder="请输入账号" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">登录IP</label>
                    <div class="layui-input-block">
                        <input type="text" name="loginIp" placeholder="请输入登录IP" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <button type="button" class="layui-btn layuiadmin-btn-useradmin" lay-submit lay-filter="search">
                        <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                    </button>
                    <button type="reset" class="layui-btn layui-btn-primary" lay-submit lay-filter="reset">重置</button>
                </div>
            </div>
        </form>
        <div class="layui-card-body">
            <table id="sysLoginLogTable" lay-filter="sysLoginLogTable"></table>
            <script type="text/html" id="toolbar">
                <div class="layui-btn-container">
                    <a sec:authorize="hasAuthority('sysLoginLog:del')" class="layui-btn layui-btn-danger layui-btn-xs" lay-event="removes"><i class="layui-icon layui-icon-delete"></i>删除</a>
                </div>
            </script>
            <script type="text/html" id="tableBar">
                <a sec:authorize="hasAuthority('sysLoginLog:del')" class="layui-btn layui-btn-danger layui-btn-xs" lay-event="remove"><i class="layui-icon layui-icon-delete"></i>删除</a>
            </script>
        </div>
    </div>
</div>
<div th:replace="common/script::footer"></div>
<script th:inline="javascript">
    layui.config({
        base: '/static/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'useradmin', 'table','crud'], function() {
        let $ = layui.$,
            form = layui.form,
            table = layui.table,
            crud = layui.crud;
        //监听搜索
        form.on('submit(search)', function(data) {
            let field = data.field;
            //执行重载
            table.reload('sysLoginLogTable', {
                where: field
            });
        });
        //监听重置
        form.on('submit(reset)', function(data) {
            Object.keys(data.field).forEach(key => (data.field[key] = ''));
            table.reload('sysLoginLogTable', {
                where: data.field
            });
        });
        // 查询列表接口
        table.render({
            elem: '#sysLoginLogTable'
            ,toolbar: '#toolbar'
            ,height: 'full-110'
            ,url:  ctx + '/sysLoginLog/page'
            ,cellMinWidth: 80
            ,page: true //开启分页
            ,cols: [[ //表头
                {type: 'checkbox', fixed: 'left'},
                {field: 'id', title: 'ID', hide:true},
                {field: 'account', title: '账号'},
                {field: 'loginIp', title: '登录IP'},
                {field: 'remark', title: '登录状态'},
                {field: 'createTime', title: '登录时间'},
                {toolbar: '#tableBar', title: '操作', width: 300, align:'center',fixed: 'right'}
            ]]
        });
        //头工具栏事件
        table.on('toolbar(sysLoginLogTable)', function(obj) {
            let checkStatus = table.checkStatus(obj.config.id)
                ,data = checkStatus.data;
            switch(obj.event) {
                case 'removes':
                    if (data.length === 0) {
                        layer.msg('请选择需要删除的行')
                    } else {
                        active.removes(data);
                    }
                    break;
            }
        });
        //监听行工具事件
        table.on('tool(sysLoginLogTable)', function(obj) {
            let data = obj.data //获得当前行数据
                ,layEvent = obj.event;
            if(layEvent === 'remove') {
                active.remove(data.id);
            }
        });
        /* 触发弹层 */
        let active = {
            /**
             * 删除
             * @param id id
             */
            remove: function(id) {
                layer.confirm('真的删除行么', function(index) {
                    //向服务端发送删除指令
                    $.ajax({
                        type: 'DELETE',
                        url:  ctx + '/sysLoginLog/remove/' + id,
                        contentType:'application/json;charset=UTF-8',
                        dataType: 'json',
                        success: function(result) {
                            layer.msg(result.message);
                            if (result.code === 200) {
                                layer.close(index);
                                table.reload('sysLoginLogTable');
                            }
                        }
                    });
                });
            },
            /**
             * 批量删除
             * @param obj 对象
             */
            removes: function(obj) {
                let ids = fun.objToArrIds(obj);
                layer.confirm('真的删除行么', function(index) {
                    //向服务端发送删除指令
                    $.ajax({
                        type: 'POST',
                        url:  ctx + '/sysLoginLog/removes',
                        data: JSON.stringify(ids),
                        contentType:'application/json;charset=UTF-8',
                        dataType: 'json',
                        success: function(result) {
                            layer.msg(result.message);
                            if (result.code === 200) {
                                layer.close(index);
                                table.reload('sysLoginLogTable');
                            }
                        }
                    });
                });
            },
        }
    });
</script>
</body>
</html>
